<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>提交采购申请</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="3">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="name">
                  <div>名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="factory_id">
                  <div>所属工厂</div>
                  <v-select v-model="data.factory_id" action="factoryList" />
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item prop="admin_id">
                  <div>申请人员</div>
                  <v-select v-model="data.admin_id" />
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item prop="status">
                  <div>状态</div>
                  <el-select v-model="data.status" size="mini">
                    <el-option v-for="(item, index) in applyStatus" :key="index" :value="index" :label="item" :disabled="true" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="3" hidden>
                <el-form-item prop="kind">
                  <div>采购物品</div>
                  <el-select v-model="data.kinds" size="mini" :disabled="true">
                    <el-option v-for="(item, index) in applyKind" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
              <div v-if="data.verify_admin">
                <el-col :span="3">
                  <el-form-item>
                    <div>验证人员</div>
                    <el-input v-model="data.verify_admin.name" :disabled="true" size="mini" />
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item>
                    <div>验证时间</div>
                    <el-input v-model="data.verify_date" :disabled="true" size="mini" />
                  </el-form-item>
                </el-col>
              </div>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="详细">
              <el-table :data="parts">
                <el-table-column v-if="data.kind === 1" label="备品备件" prop="parts.name" min-width="16%" />
                <el-table-column v-if="data.kind === 2" label="工具" prop="tools.name" min-width="16%" />
                <el-table-column label="供应商" prop="provider" min-width="13%" />
                <el-table-column v-if="data.kind === 1" label="参考编号" prop="parts.coding" min-width="12%" />
                <el-table-column v-if="data.kind === 2" label="参考编号" prop="tools.coding" min-width="12%" />
                <el-table-column label="注意" prop="notice" min-width="13%" />
                <el-table-column label="数量" prop="number" min-width="13%" />
                <el-table-column label="单价 (¥)" prop="price" min-width="12%" />
                <el-table-column label="需要日期" prop="datetime" min-width="13%" />
                <el-table-column label="操作" min-width="8%">
                  <template v-slot="scope">
                    <el-button type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="append_form" :model="order" :inline="true" class="hf_form append_other_admin" :rules="appendRules">
                    <el-row :gutter="10">
                      <el-col :span="4">
                        <el-form-item prop="parts_id">
                          <v-select v-if="data.kind === 1" v-model="order.parts_id" action="partsList" />
                          <v-select v-if="data.kind === 2" v-model="order.parts_id" action="toolList" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="3">
                        <el-form-item prop="provider_id">
                          <v-select v-if="data.kind === 1" v-model="order.provider_id" action="providerList" :disabled="!order.parts_id" :query="{part_id:order.parts_id,kind:1}" />
                          <v-select v-if="data.kind === 2" v-model="order.provider_id" action="providerList" :disabled="!order.parts_id" :query="{part_id:order.parts_id,kind:2}" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="3">
                        <el-form-item>
                          <el-input v-model="order.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="3">
                        <el-form-item prop="notice">
                          <el-input v-model="order.notice" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="3">
                        <el-form-item prop="number">
                          <el-input v-model="order.number" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="3">
                        <el-form-item prop="price">
                          <el-input v-model="order.price" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="3">
                        <el-form-item prop="datetime">
                          <el-date-picker v-model="order.datetime" type="date" value-format="yyyy-MM-dd" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="append" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row :gutter="10">
                <el-col :span="4">
                  <div>
                    <span>创建时间日期</span>
                    <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>创建人</span>
                    <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div v-if="data.update_time">
                    <div>
                      <span>最近更新</span>
                      <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                    </div>
                    <div>
                      <span>更新者</span>
                      <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                    </div>
                  </div>
                </el-col>
                <el-col :span="20">
                  <wangeditor id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/tool/apply'
import Wangeditor from '@/components/editor/wangeditor'
import { applyStatus, applyKind } from '@/utils/options'
import VSelect from '@/components/select'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendParts', 'removeParts'],
  components: {
    VSelect,
    Wangeditor
  },
  data() {
    return {
      data: {
        'cr_admin': {
          'name': ''
        },
        'verify_admin': {
          'name': ''
        },
        'kinds': 1
      },
      parts: [],
      order: {},
      applyStatus,
      applyKind,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入申请单名称',
            trigger: 'blur'
          }
        ],
        factory_id: [
          {
            required: true,
            message: '请选择工厂',
            trigger: 'change'
          }
        ],
        admin_id: [
          {
            required: true,
            message: '请选择维护负责人',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      appendRules: {
        parts_id: [
          {
            required: true,
            message: '请选择采购物品',
            trigger: 'change'
          }
        ],
        provider_id: [
          {
            required: true,
            message: '请选择供应商',
            trigger: 'change'
          }
        ],
        notice: [
          {
            required: true,
            message: '请填写注意事项',
            trigger: 'blur'
          }
        ],
        number: [
          {
            required: true,
            message: '请填写数量',
            trigger: 'blur'
          }
        ],
        price: [
          {
            required: true,
            message: '请填写价格',
            trigger: 'blur'
          }
        ],
        datetime: [
          {
            required: true,
            message: '请填写日期',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 51404) {
        this.$alert('您请求的采购申请单不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      const kind = response.data.kind
      if (kind === 1) {
        this.parts = response.data.part
      } else if (kind === 2) {
        this.parts = response.data.tool
      }
      this.data.kinds = response.data.kind - 1
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.append_form
      const parts_data = {
        'parts_id': this.order.parts_id,
        'provider_id': this.order.provider_id,
        'notice': this.order.notice,
        'number': this.order.number,
        'price': this.order.price,
        'datetime': this.order.datetime,
        'applyFor_id': this.$route.params.id
      }
      const errorCode = 51004
      const errorMessage = '该备件已存在'
      this.appendParts(API, thisForm, parts_data, errorCode, errorMessage)
    },
    remove(id) {
      const parts_data = {
        'id': id
      }
      this.removeParts(API, id, parts_data)
    }
  }
}
</script>
